<template>
    <div class="shouye">
        <div class="shouye-cover slidedown">
            <div class="shouye-intro">
                <h1 class="shouye-title scaleup">{{ siteInfo.siteTitle && siteInfo.siteTitle.toUpperCase() }}</h1>
                <div :class="['scaleup', { 'shouye-desc': shouyeDesc }]">
                    <span v-html="words"></span>
                    <span>|</span>
                </div>
            </div>
            <div class="arrow-down" @click="scrollDown">
                <i class="iconfont icon-a-xiala2"></i>
            </div>
        </div>
        <div class="shouye-content slideup">

            <div class="content-wrapper">

               <!--  <a href="#" class="shuoshuo shadow scaleup">
                    <i class='iconfont icon-message'></i>
                    <div style="width: 100%;text-align: center;">follow your heart and get it~</div>
                    <i class='iconfont icon-youshuangjiantou arrow-right-move'></i>
                </a> -->

                <div v-for="article, idx in articleList" :key="article.id" class="content">
                    <div class="article-item shadow scaleup">
                        <router-link :to="`/post/${article.id}`" class="article-cover" :style="{ order: idx % 2 }">
                            <img :src="article.articleCover">
                        </router-link>
                        <div class="article-info">
                            <router-link :to="`/post/${article.id}`" class="article-title">{{ article.articleTitle }}</router-link>
                            <div class="article-intro">
                                <i class="iconfont icon-rili"></i>
                                <span>{{ article.publishTime }}</span>
                                <span class="verticle-line">|</span>
                                <i class="iconfont icon-wenjian"></i><span>{{ article.categoryDTO.categoryName }}</span>
                                <span class="verticle-line">|</span>
                                <template v-if="article.tagDTOList && article.tagDTOList.length > 0">
                                    <span v-for="tagDTO in article.tagDTOList" :key="tagDTO.id" class="mr5">
                                        <i class="iconfont icon-biaoqian"></i>
                                        <span>{{tagDTO.tagName}}</span>
                                    </span>
                                </template>
                            </div>
                            <div class="article-text" v-html="article.articleContent">
                                
                            </div>
                        </div>
                    </div>
                </div>

                <div class="loading-area">

                    <div class="loading-effect" v-show="hasMore">
                        <div class="loading-text" v-show="!isLoading" ref="loadingTextRef">
                            正在加载中...<!-- {{ 'isLoading=' + isLoading }} - {{ 'hasMore=' + hasMore }} -->
                        </div>
                        <div class="loading-animation" v-show="isLoading">
                            <Loading />
                        </div>
                    </div>

                    <div class="bottom-line" v-show="!hasMore">
                        <span>我也是有底线的</span>
                    </div>

                </div>

            </div>


            <div class="sider-wrapper" id="sider">

                <div class="sider-sticky">

                    <div class="author shadow ">
                        <a href="#" class="avatar rotateForever">
                            <img :src="siteInfo.avatarUrl" alt="">
                        </a>
                        <div class="author-name">
                            {{ siteInfo.blogAuthor }}
                        </div>
                        <div class="author-bio">
                            {{ siteInfo.bio }}
                        </div>
                        <div class="author-statis">
                            <a href="#" class="author-statis-item">
                                <div class="author-statis-item-name">文章</div>
                                <div>{{ siteInfo.articleCount }}</div>
                            </a>
                            <a href="#" class="author-statis-item">
                                <div class="author-statis-item-name">分类</div>
                                <div>{{ siteInfo.categoryCount }}</div>
                            </a>
                            <a href="#" class="author-statis-item">
                                <div class="author-statis-item-name">标签</div>
                                <div>{{ siteInfo.tagCount }}</div>
                            </a>
                        </div>

                        <div class="join-label animate__animated" @click="addToBookMark">
                            <i class="iconfont icon-24gl-bookmark"></i>
                            加入书签
                        </div>

                        <div class="author-links">
                            <a href="#">
                                <i class="iconfont icon-QQ-circle-fill qq"></i>
                            </a>
                            <a href="https://gitee.com/zzhua195" target="_blank">
                                <i class="iconfont icon-gitee-fill-round gitee"></i>
                            </a>
                            <a href="#">
                                <i class="iconfont icon-weibo weibo"></i>
                            </a>
                        </div>

                    </div>

                    <div class="notice shadow ">
                        <div class="notice-header">
                            <i class="iconfont icon-gonggao wave"></i>公告
                        </div>
                        <div class="notice-content" v-html="siteInfo.notice"></div>
                    </div>

                    <div class="site-info shadow ">
                        <div class="site-info-header">
                            <i class="iconfont icon-tongji"></i>网站资讯
                        </div>
                        <div class="site-info-content">
                            <div class="site-info-item">
                                <span>运行时间: </span>
                                <span>{{ time }}</span>
                            </div>
                            <!-- <div class="site-info-item">
                                <span>总访问量: </span>
                                <span>102</span>
                            </div> -->
                        </div>
                    </div>
                </div>

            </div>

        </div>
        <Footer></Footer>
    </div>
</template>

<script>
import Footer from '@/components/Footer.vue'
import Loading from '@/components/Loading/Loading'
import { getArticleList as getArticleListApi, } from '@/api/homeApi'

import md from '@/utils/markdown'

export default {
    name: 'Home',
    data() {
        return {
            typeWords: '',
            words: '',
            i: 0,
            shouyeDesc: false,
            time: '',
            currPage: 1,
            pageSize: 2,
            articleList: [],
            hasMore: true,
            isLoading: false
        }
    },
    created() {
        fetch('https://v1.hitokoto.cn/?c=i')
            .then(res => {
                return res.json();
            })
            .then(json => {
                this.typeWords = json.hitokoto
            })
        setInterval(this.runTime, 1000)
    },
    mounted() {
        this.typing()

        let observer = new IntersectionObserver(entries => {
            for (let entry of entries) {
                // console.log(entry,'entry');
                if (entry.isIntersecting) { // 当正在加载的文字出现的时候, 才开始发起请求, 加载数据
                    // console.log(this, 'this');
                    this.getArticleList(observer)
                }
            }
        }, { threshold: 1 })
        observer.observe(this.$refs['loadingTextRef'])

    },
    computed: {
        siteInfo() {
            return this.$store.state.home.siteInfo
        }
    },
    methods: {
        addToBookMark() {
            this.$toast('success', '按CTRL + D收藏本站')
        },
        typing() {
            // console.log(this.words, 'words');
            if (this.i <= this.typeWords.length) {
                this.words = this.typeWords.slice(0, this.i)
                this.i++
                let timeout = setTimeout(() => {
                    this.typing()
                }, 300)
            } else {
                this.shouyeDesc = true
            }
        },
        scrollDown() {
            window.scroll({
                top: document.documentElement.clientHeight,
                behavior: 'smooth'
            })
        },
        runTime() {
            var timeold =
                new Date().getTime() -
                new Date(this.siteInfo.createTime).getTime();
            var msPerDay = 24 * 60 * 60 * 1000;
            var daysold = Math.floor(timeold / msPerDay);
            var str = "";
            var day = new Date();
            if (daysold != 0) {
                str += daysold + "天";
            }
            str += day.getHours() + "时";
            str += day.getMinutes() + "分";
            str += day.getSeconds() + "秒";
            this.time = str;
        },
        getArticleList(observer) {
            this.isLoading = true
            getArticleListApi({ currPage: this.currPage, pageSize: this.pageSize, data: {} }).then(res => {

                (res.list || []).forEach(article=>{
                    article.articleContent = md.render(article.mdContent)
                    article.articleContent = article.articleContent.replace(/<\/?[^>]*>/g,'')
                })

                // console.log(res.list);

                this.articleList.splice(this.articleList.length,0,...res.list)
                this.isLoading = false
                this.$nextTick(() => {
                    if (res.totalCount > this.currPage * res.pageSize) { // 证明还有数据, 还可以继续加载
                        this.hasMore = true
                        this.currPage++
                    } else {
                        if(this.$refs['loadingTextRef']) {
                            observer.unobserve(this.$refs['loadingTextRef'])
                        }
                        this.hasMore = false // 当前页已经是最后一页了, 后面没有更多数据了
                    }
                })
            })
        }
    },
    components: {
        Footer,
        Loading,
    }
}
</script>

<style lang="scss" scoped>
/* 封面图下移效果 */
@keyframes slidedown {
    0% {
        opacity: 0.3;
        transform: translateY(-60px);
    }

    100% {
        opacity: 1;
        transform: translateY(0px);
    }
}

.slidedown {
    animation: slidedown 1s;
}

/* 内容上移效果 */
@keyframes slideup {
    0% {
        opacity: 0.3;
        // transform: translateY(60px);
    }

    100% {
        opacity: 1;
        // transform: translateY(0px);
    }
}

.slideup {
    animation: slideup 1s;
}

/* 从小变大效果 */
@keyframes scaleup {
    0% {
        transform: scale(0.3);
    }

    100% {
        transform: scale(1);
    }
}

.scaleup {
    animation: scaleup 1s;
}

/* 箭头下滑 */
@keyframes arrowSlideDown {
    0% {
        opacity: 0;
        transform: translateY(-20px);
    }

    50% {
        opacity: 1;
        transform: translateY(0px);
    }

    100% {
        opacity: 0;
        transform: translateY(20px);
    }
}

/* 光标闪烁 */
@keyframes shanshuo {
    0% {
        opacity: 0;
    }

    50% {
        opacity: 1;
    }

    100% {
        opacity: 0;
    }
}

/* 箭头滑向右边 */
@keyframes arrowSlideRight {
    0% {
        opacity: 0;
        transform: translateX(-20px);
    }

    50% {
        opacity: 1;
        transform: translateX(0px);
    }

    100% {
        opacity: 0;
        transform: translateX(5px);
    }
}

.arrow-right-move {
    animation: arrowSlideRight 3s infinite;
}

/* 头像旋转 */
@keyframes rotateAround {
    0% {
        transform: rotate(0deg);
    }

    100% {
        transform: rotate(360deg);
    }
}

.rotateForever {
    animation: rotateAround 5s linear infinite;
}

.avatar:hover {
    animation-play-state: paused;
}

/* 公告摆动 */
@keyframes wave {
    0% {
        transform: rotate(350deg);
    }

    100% {
        transform: rotate(370deg);
    }
}

.wave {
    animation: wave 1.3s infinite ease-in-out alternate-reverse;
    transform-origin: 50% 0;
    display: inline-block;
}

.shouye-cover {
    height: 100vh;
    width: 100vw;
    background-image: url(@/assets/img/bg/home-bg.jpg);
    background-size: cover;
    background-position: center;
    position: relative;

    display: flex;

    color: #eee;

    .shouye-intro {
        margin-top: 40vh;
        width: 100%;
        text-align: center;

        font-size: 18px;

        .shouye-title {
            margin-bottom: 10px;
        }

        .shouye-desc {
            span:nth-child(2) {
                animation: shanshuo 1s infinite;
            }
        }

    }

    .arrow-down {
        position: absolute;
        width: 100%;
        height: 90px;
        bottom: 0;
        text-align: center;

        i {
            font-size: 32px;
            cursor: pointer;
            display: inline-block;
            animation: arrowSlideDown 2s infinite;
        }
    }


}

.shouye-content {
    max-width: 1200px;
    // border: 1px solid red;
    margin: 0 auto 20px;
    padding: 10px;
    display: flex;
    color: #595655;

    .content-wrapper {
        width: 75%;
        padding: 12px;
        // border: 1px solid red;

        .shuoshuo {
            background-color: #fff;
            padding: 10px;
            display: flex;
            cursor: pointer;
            border-radius: 8px;
            margin-bottom: 20px;
        }

        .content:last-child {
            .article-item {
                margin-bottom: 0;
            }
        }

        .content {

            .article-item {
                margin-bottom: 20px;
                display: flex;
                align-items: center;
                height: 300px;
                background-color: #fff;
                border-radius: 8px;
                overflow: hidden;

                .article-cover {
                    width: 45%;
                    height: 100%;

                    overflow: hidden;

                    img {
                        width: 100%;
                        height: 100%;
                        transition: all 0.5s;

                        object-fit: cov;

                        &:hover {
                            transform: scale(1.1);
                        }
                    }
                }

                .article-info {
                    width: 55%;
                    margin: 0 20px;

                    .article-title {
                        font-size: 24px;
                        cursor: pointer;
                        transition: all 0.3s;

                        display: block;

                        &:hover {
                            color: #30adff;
                        }
                    }

                    .article-intro {
                        font-size: 12px;
                        margin: 8px 0;
                        color: #a7a7a7;
                        display: inline-flex;
                        align-items: center;
                        margin-left: 5px;

                        flex-wrap: wrap;

                        i {
                            margin-right: 5px;
                        }

                    }

                    .article-text {
                        text-indent: 2em;
                        display: -webkit-box;
                        -webkit-box-orient: vertical;
                        -webkit-line-clamp: 3;
                        overflow: hidden;
                    }
                }

                .article-text {
                    word-break: break-all;
                }
            }
        }
    }

    .sider-wrapper {
        width: 25%;
        // border: 1px solid red;
        padding: 12px;
        padding-bottom: 73px;

        .sider-sticky {
            position: sticky;
            top: 20px;
        }

        .author {
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            display: flex;
            flex-direction: column;
            align-items: center;
            box-shadow: 1px 0 0 0 rgba(0, 0, 0, 0.3);
            line-height: 1.5;
            margin-bottom: 15px;

            .avatar {
                width: 116px;
                height: 116px;
                display: block;
                border-radius: 50%;
                overflow: hidden;
                border: 2px solid rgb(230, 230, 230);

                img {
                    width: 100%;
                    height: 100%;
                }
            }

            .author-name {
                color: #4c4948;
                font-size: 24px;

            }

            .author-bio {
                font-size: 14px;
                margin-top: 10px;
            }

            .author-statis {
                display: flex;
                margin-top: 10px;
                width: 100%;
                padding: 0 10px;

                .author-statis-item {
                    flex: 1;
                    display: flex;
                    flex-direction: column;
                    align-items: center;

                    .author-statis-item-name {
                        margin-bottom: 10px;
                    }
                }
            }

            .join-label {
                height: 30px;
                background-color: #49b1f5;
                width: 100%;
                text-align: center;
                line-height: 30px;
                color: #fff;
                font-size: 0.845em;
                margin-top: 10px;
                cursor: pointer;
                z-index: 1;

                position: relative;

                i {
                    font-size: 0.875em;
                }

                &:hover {
                    animation-name: pulse;
                }

            }

            .author-links {
                margin-top: 10px;
                display: flex;

                a {
                    // border: 1px solid red;
                    display: block;
                    margin: 0 5px;
                    height: 1.8em;
                    width: 1.8em;
                    display: flex;
                    align-items: center;
                    justify-content: center;


                    i {
                        font-size: 1.6em;

                        &.gitee {
                            font-size: 1.4em !important;
                            color: #c71d23;
                        }

                        &.weibo {
                            font-size: 1.8em !important;
                        }

                        &.qq {
                            color: #08bdfd;
                        }
                    }
                }

            }
        }

        .notice {
            padding: 20px;
            background-color: #fff;
            margin-bottom: 15px;
            border-radius: 8px;

            line-height: 2;

            .notice-header {
                margin-bottom: 5px;
            }

            i {
                margin-right: 5px;
            }
        }

        .site-info {
            padding: 20px;
            background-color: #fff;
            border-radius: 8px;

            line-height: 2;

            .site-info-header {
                margin-bottom: 5px;
            }

            i {
                margin-right: 5px;
            }

            .site-info-content {
                .site-info-item {
                    display: flex;
                    justify-content: space-between;
                }
            }

        }

    }

}

.loading-area {
    .loading-effect {
        border-radius: 8px;

        height: 50px;
        // border: 1px solid red;
        text-align: center;

        &>div {
            width: 100%;
            height: 100%;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .loading-animation {
            position: relative;
        }
    }

    .bottom-line {
        height: 40px;
        text-align: center;
        position: relative;
        display: flex;
        align-items: center;
        justify-content: center;
        // border: 1px solid red;

        span {
            padding: 0 12px;
            z-index: 1;
        }

        &::before {
            content: '';
            position: absolute;
            width: 40%;
            border-bottom: 1px dashed #ccc;
            top: 20px;
            left: 0;
        }

        &::after {
            content: '';
            position: absolute;
            width: 40%;
            border-bottom: 1px dashed #ccc;
            top: 20px;
            right: 0;
        }
    }
}

.notice-content {
    display: flex;
    align-items: center;
}
</style>